from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound, HttpResponseNotAllowed
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login
from django.template import RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from intr.models.models import *
from intr.events.models import Event
from intr.blog.models import BlogPost
from intr.societies.forms import SocietyForm
from intr.facebook_utils import *

def societies(request):
	societies = Society.objects.all()
	return render_to_response('societies/societies.html', {'societies':societies}, context_instance=RequestContext(request))

def society(request, slug):
	society = Society.objects.get(slug=slug)
	posts = BlogPost.objects.filter(society=society)
	if posts.count() > 0:
		post = posts[0]
	else:
		post = None
	
	members = Membership.objects.filter(society=society, public=True).order_by()
	return render_to_response('societies/society.html', {'society':society, 'post':post, 'members':members}, context_instance=RequestContext(request))

def society_blog(request, slug, page=0):
	society = Society.objects.get(slug=slug)
	posts = BlogPost.objects.filter(society=society).order_by('added')
	
	return render_to_response('societies/blog.html', {'society':society, 'posts':posts}, context_instance=RequestContext(request))


def society_events(request, slug, week=0):
	society = Society.objects.get(slug=slug)
	events = Event.objects.filter(society=society).order_by('start')
	
	return render_to_response('societies/events.html', {'society':society, 'events':events}, context_instance=RequestContext(request))
	

def leave(request, slug):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/societies/%s' % slug)
	
	society = Society.objects.get(slug=slug)
	
	if request.user not in society.members.all():
		return HttpResponseRedirect('/societies/%s' % slug)
	
	membership = request.user.membership_set.get(society=society)
	membership.delete()
	
	return HttpResponseRedirect('/societies/%s' % slug) 


def join(request, slug):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/societies/%s' % slug)
	
	society = Society.objects.get(slug=slug)
	
	if request.user in society.members.all():
		return HttpResponseRedirect('/societies/%s' % slug)
	
	membership = Membership()
	membership.member = request.user
	membership.society = society
	membership.public = True
	membership.save()
	
	return HttpResponseRedirect('/societies/%s' % slug)


def settings(request, slug):
	if not request.user.is_authenticated():
		return HttpResponseRedirect('/societies/%s' % slug)
	
	society = Society.objects.get(slug=slug)
	membership = request.user.membership_set.filter(society=society)
	if membership: membership = membership[0]
	
	if not membership.society == society or not membership.position:
		return HttpResponseRedirect('/societies/%s' % slug)
		
	if request.POST.get('page_select', None):
		data = request.POST.get('page_select', None).split(':')
		society.fb_page_id = data[0]
		society.fb_page_access_token = data[1]
		society.save()
	
	if request.POST:
		society_form = SocietyForm(request.POST, instance=society)
		if society_form.is_valid():
			society_form.save()
			return HttpResponseRedirect('/societies/%s' % slug)
	else:
		society_form = SocietyForm(instance=society)
	
	facebook_auth_url = create_request_url(society.slug)
	
	if not society.fb_page_id and society.fb_access_token:
		page_options = get_all_pages(society.fb_access_token)
		page_options = filter(lambda x: x['category'] != 'Application', page_options)
		
		return render_to_response('societies/edit.html', {'form':society_form, 'society':society, 'facebook_auth_url':facebook_auth_url, 'page_options':page_options}, context_instance=RequestContext(request))
	
	return render_to_response('societies/edit.html', {'form':society_form, 'society':society, 'facebook_auth_url':facebook_auth_url}, context_instance=RequestContext(request))


def facebook_callback(request, slug):
	society = get_object_or_404(Society, slug=slug)
	return render_to_response('societies/callback.html', {'society':society})


def facebook_access_token(request, slug):
	society = get_object_or_404(Society, slug=slug)
	society.fb_access_token = request.GET.get('access_token', None)
	society.save()
	return HttpResponse('')


def members(request, slug):
	society = Society.objects.get(slug=slug)
	members = Membership.objects.filter(society=society).order_by()
	return render_to_response('societies/membership_list.html', {'members':members, 'society':society}, context_instance=RequestContext(request))